package com.sunwayworld.common.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sunwayworld.common.utils.SessionUtils;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2002
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author unascribed
 * @version 1.0
 */

// public class LogonFilter extends HttpServlet implements Filter {
public class LoginFilter implements Filter {
	/**
	 * 默认的Encoding，也是在web.xml中配置的参数
	 */
	protected String encoding = null;

	protected FilterConfig filterConfig = null;

	// Handle the passed-in FilterConfig
	public void init(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;

	}

	// Process the request/response pair
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) {
		try {

			HttpServletRequest httpRequest = (HttpServletRequest) request;
			
			String curRequestURL = httpRequest.getRequestURL().toString() + "?"
					+ httpRequest.getQueryString();

			if (SessionUtils.isLogin(httpRequest)) {// 如果用户已经登录，并SESSION有效

				filterChain.doFilter(request, response);//
			} else {// 没有用户登录
				if (notNeedFilterUri(curRequestURL, request)) { // 设定不需要过滤的URI
					filterChain.doFilter(request, response);
				} else {

					String strLogonUri = httpRequest.getContextPath()
							+ "/loginAction.do";
					HttpServletResponse httpResponse = (HttpServletResponse) response;
					httpResponse.sendRedirect(strLogonUri);
				}
			}
		} catch (ServletException sx) {
			filterConfig.getServletContext().log(sx.getMessage());
		} catch (IOException iox) {
			filterConfig.getServletContext().log(iox.getMessage());
		}
	}

	/**
	 * 看是否需要过滤,以中断过滤链
	 * 
	 * @param uri
	 * @return
	 */
	private boolean notNeedFilterUri(String uri, ServletRequest request) {

		try {
			// String method = request.getParameter("method");
			boolean flag = false;

			if (uri.indexOf(".gif") >= 0 || uri.indexOf(".jpg") >= 0
					|| uri.indexOf(".css") >= 0 || uri.indexOf(".js") >= 0) {
				flag = true;
			}

			if (uri.indexOf("loginAction.do") >= 0) {

				flag = true;
			}
			return flag;

		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}

	// Clean up resources
	public void destroy() {
		this.encoding = null;
		this.filterConfig = null;
	}

	/**
	 * Select an appropriate character encoding to be used, based on the
	 * characteristics of the current request and/or filter initialization
	 * parameters. If no character encoding should be set, return
	 * <code>null</code>.
	 * <p>
	 * The default implementation unconditionally returns the value configured
	 * by the <strong>encoding</strong> initialization parameter for this
	 * filter.
	 * 
	 * @param request
	 *            The servlet request we are processing
	 */
	protected String selectEncoding(ServletRequest request) {
		return (this.encoding);
	}

	/**
	 * Returns the filterConfig.
	 * 
	 * @return FilterConfig
	 */
	public FilterConfig getFilterConfig() {
		return filterConfig;
	}

	/**
	 * Sets the filterConfig.
	 * 
	 * @param filterConfig
	 *            The filterConfig to set
	 */
	public void setFilterConfig(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;
	}

}
